package com.focus.pay.ali.action.order;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.WebUtils;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.focus.pay.ali.config.AliPayConfig;
import com.focus.pay.ali.constant.Constant;
import com.focus.pay.ali.constant.AliChannelType;
import com.focus.pay.core.enums.Channel;
import com.focus.pay.ali.model.order.AliPayModelCodeBar;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.exception.ExceptionEnum;
import com.focus.pay.core.exception.PayException;

/**
 * @Author: 赵参谋
 * @Date: 2021/9/28 17:41
 * @Description: 支付宝条码付款
 * 用户展示二维码,扫码枪/收费机扫码付款
 */
@PayOrderHandler(channel = Channel.AliPay,channelType = AliChannelType.CodeBar)
public class AliPayOrderCodeBar extends AliPayOrder {

    /**
     * 统一收单交易支付接口
     * @param aliPayModelCodeBar 条码付款信息
     * @param aliPayConfig 支付配置
     * @return 条码支付结果
     */
    public AlipayTradePayResponse  payOrder(AliPayModelCodeBar aliPayModelCodeBar, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradePayRequest request = new AlipayTradePayRequest();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelCodeBar));
            AlipayTradePayResponse  response = alipayClient.execute(request);
            return response;
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }
}
